package com.ssh.admin.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Activity;
import com.ssh.entity.Brand;
import com.ssh.manager.ActivityManager;
import com.ssh.manager.BrandManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/activity/activity.jsp"),
@Result(name="findList", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.subject,page\\.pageData\\[\\d+\\]\\.content,page\\.pageData\\[\\d+\\]\\.createDate," +
"page\\.pageData\\[\\d+\\]\\.startDate,page\\.pageData\\[\\d+\\]\\.endDate,page\\.pageData\\[\\d+\\]\\.brand\\.name,page\\.pageData\\[\\d+\\]\\.brand\\.id"}),
@Result(name="update", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="delete", type="json", params={"root","page","excludeproperties","page\\.curPageNum"})
})
public class ActivityAdminAction extends ActionSupport{
	@Resource(name="activityManagerImpl")
	private ActivityManager activityManager;
	@Resource(name="brandManagerImpl")
	private BrandManager brandManager;

	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private String isDesc;
	private Activity activity;
	private Brand brand;
	private String ids;
	
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public Brand getBrand() {
		return brand;
	}
	public void setBrand(Brand brand) {
		this.brand = brand;
	}
	public Activity getActivity() {
		return activity;
	}
	public void setActivity(Activity activity) {
		this.activity = activity;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public JsonResult getJr() {
		return jr;
	}
	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	public String getIsDesc() {
		return isDesc;
	}
	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}
	
	public String goJsp(){
		return "success";
	}
	
	public String add(){
		brand = brandManager.getBrandById(brand.getId());
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = df.format(activity.getEndDate());
		str = str.substring(0, 10);
		str += " 23:59:59";
		Timestamp ts = Timestamp.valueOf(str);
		activity.setEndDate(ts);
		
		activity.setBrand(brand);
		activity.setDel(0);
		activity.setCreateDate(new Timestamp(System.currentTimeMillis()));
		activityManager.saveUpdate(activity);
		return "success";
	}
	
	public String findList(){
		Map conditionMap = new HashMap();
		conditionMap.put("brand", brand.getId());
		activityManager.findList(conditionMap, page, isDesc);
		return "findList";
	}
	
	public String update(){
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str = df.format(activity.getEndDate());
			str = str.substring(0, 10);
			str += " 23:59:59";
			Timestamp ts = Timestamp.valueOf(str);
			
			String startStr = df.format(activity.getStartDate());
			Timestamp startTs = Timestamp.valueOf(startStr);
			
			Activity ac = activityManager.getById(activity.getId());
			ac.setBrand(brandManager.getBrandById(brand.getId()));
			ac.setSubject(activity.getSubject());
			ac.setContent(activity.getContent());
			ac.setStartDate(startTs);
			ac.setEndDate(ts);
			activityManager.saveUpdate(ac);
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			e.printStackTrace();
		}
		return "update";
	}
	
	public String delete(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					activityManager.delete(new Long(d[i]));
				}
			}else{
				activityManager.delete(activity.getId());
			}
			int _p = (int) page.getCurPageNum();
			Map conditionMap = new HashMap();
			conditionMap.put("brand", brand.getId());
			page.setCurPageNum(1);
			activityManager.findList(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			e.printStackTrace();
		}
		return "delete";
	}
}
